package com.zsh.weather.ln.home.ui.home
import android.os.Handler
import android.os.Message
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yechaoa.yutilskt.LogUtilKt
import com.zsh.weather.ln.R
import com.zsh.weather.ln.base.BaseMVPFragment
import com.zsh.weather.ln.bean.IndicesBase
import com.zsh.weather.ln.bean.IndicesInfo
import com.zsh.weather.ln.bean.NowBase
import com.zsh.weather.ln.bean.NowInfo
import com.zsh.weather.ln.home.adapter.HomeAdapter
import com.zsh.weather.ln.home.ui.home.contract.HomeContract
import com.zsh.weather.ln.home.ui.home.presenter.HomePresenter
import com.zsh.weather.ln.utils.Constant
import com.zsh.weather.ln.utils.Utils

class HomeFragment : BaseMVPFragment<HomeContract.View, HomePresenter>(), HomeContract.View {
    private lateinit var rv_view: RecyclerView
    private lateinit var tv_temp: TextView
    private lateinit var tv_stuation: TextView
    private lateinit var tv_content: TextView
    private lateinit var tv_more: TextView
    private lateinit var url: String

    override fun initView(v: View) {
        tv_temp = v.findViewById(R.id.tv_temp)
        rv_view = v.findViewById(R.id.rv_view)
        tv_stuation = v.findViewById(R.id.tv_stuation)
        tv_content = v.findViewById(R.id.tv_content)
        tv_more = v.findViewById(R.id.tv_more)
        rv_view.layoutManager = LinearLayoutManager(mContext)
        click()
    }

    private fun click() {
        tv_more.setOnClickListener {
            Utils.openUrl(mContext, "accu")
        }
    }

    var mHandler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1 -> {
                    var nowInfo = msg.obj as NowInfo
                    tv_temp.text = nowInfo.temp
                    tv_stuation.text = nowInfo.text
                    tv_content.text = "${nowInfo.windDir}  ${nowInfo.windScale}级"
                }
                2 -> {
                    var list = msg.obj as MutableList<IndicesInfo>
                    rv_view.adapter = HomeAdapter(list)
                }
            }
        }
    }

    override fun initData() {
        super.initData()
        LogUtilKt.d("thread_main:" + Thread().name)
        presenter.getNowWeather()
    }

    override fun indices(indicesBase: IndicesBase) {
        var message = Message.obtain()
        message.obj = indicesBase.daily
        message.what = 2
        mHandler.sendMessage(message)

    }

    override fun nowWeather(result: NowBase) {
        url = result.fxLink
        var message = Message.obtain()
        message.obj = result.now
        message.what = 1
        mHandler.sendMessage(message)
    }

    override fun createPresenter(): HomePresenter {
        return HomePresenter()
    }

    override fun showLoading() {
    }

    override fun hideLoading() {
    }

    override fun error(msg: String) {
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_home
    }
}